#encoding=utf-8
from __future__ import division

import sys
import os
import datetime
import time
import math
import ujson
import uuid
import urllib
from urlparse import urlparse, parse_qsl

import requests
from django.contrib import auth
from django.template import RequestContext
from django.core.cache import cache
from django_redis import get_redis_connection
from django.core.servers.basehttp import FileWrapper
from django.http import StreamingHttpResponse
from django.shortcuts import render_to_response, HttpResponse, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.models import User
from django.db.models import Count, Max, F, Q
from django.contrib.auth.decorators import login_required

from andy_manager import settings
from websys.models import (
    UserProfile, HXHUserInfo, HXHPayInfo,HXHUserLogin,HXHConsumerInfo,
)

import  websys.fun as fun

from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
     #猎人注册
    url(r'^account', 'websys.view.hxhunter.hxh_user_add', name='hxh_user_add'),
     #猎人消费
    url(r'^consumer', 'websys.view.hxhunter.hxh_user_consumer', name='hxh_user_consumer'),
    #猎人玩家登陆
    url(r'^login', 'websys.view.hxhunter.hxh_user_login', name='hxh_user_login'),
    url(r'^online', 'websys.view.hxhunter.hxh_user_online', name='hxh_user_online'),
    )

#用户添加http://andytest.nvzhanshen.com:8998/api/hxhunter/account?
# reason=NEW_CREATE&
# gid=hxhunter&
# uid=d8f33838264a11e7994f5254000d335c&
# area=s1&
# data=%5B%7B%22username%22%3A%22%5Cu57c3%5Cu4f26%5Cu00b7%5Cu8fea%5Cu80af%5Cu65af%22%2C%22account%22%3A%22TEST%40laya1643911%22%2C%22created%22%3A1492748856.7909090519%7D%5D
# &pid=TEST&
# order=d8fbf068264a11e7994f5254000d335c
#http://127.0.0.1:8080/api/hxhunter/account?reason=NEW_CREATE&gid=hxhunter&uid=abvcd5838264a11e7624f5254000d335c&area=qq1&data=%5B%7B%22username%22%3A%22%5Cu57c3%5Cu4f26%5Cu00b7%5Cu8fea%5Cu80af%5Cu65af%22%2C%22account%22%3A%22TEST%40lw21qqwa1643968%22%2C%22created%22%3A1492749956.7909090519%7D%5D&pid=TEST&order=d8fbf068264a11e7994f5254000d335c
@csrf_exempt
def hxh_user_add(request):
    t_obj=fun.Decode_Request(request)
    fun.add_log(t_obj)

    t_uid=None
    t_data={}
    if request.method == 'GET':
        t_uid = t_obj['uid']#request.GET.get('uid')
        t_area=t_obj['area']#request.GET.get('area')#区服
        t_pid=t_obj['pid']#平台
        t_data=ujson.loads(t_obj['data'])[0]
    elif request.method == 'POST':
        t_uid = request.POST.get('uid')

    if not t_uid:
        response = HttpResponse("empty uid")
        return response

    t_name = t_data["username"]
    t_account = t_data["account"]
    t_created=t_data["created"]
    #赋值
    user_info = HXHUserInfo()
    user_info.uid = t_uid
    user_info.account=t_account
    user_info.username=t_name
    user_info.pf = t_pid
    user_info.pf1 = t_pid+'1'
    user_info.server_id = t_area
    user_info.body = ''
    t_time = time.localtime(int(t_created))
    user_info.create_date = time.strftime('%Y-%m-%d %H:%M:%S',t_time)#时间截转化
    user_info.save()
    response = HttpResponse("result:"+str(user_info.id))
    return  response

#购买房卡
# uid u'abvcd5838264a11e7624f5254000d335c'
# area u's1'
# pid u'TEST'
# reason u'NEW_CREATE'
# gid u'hxhunter'
# data u'[{"amt":200000,"unit":"gamepoint","created":1492748856.7657520771},{"amt":300,"unit":"point","created":1492748856.7891640663}]'
# order u'd8fc42ac264a11e7994f5254000d335c'
@csrf_exempt
def hxh_user_consumer(request):
    t_obj=fun.Decode_Request(request)
    fun.add_log(t_obj)

    t_uid=None
    t_data=[]
    if request.method == 'GET':
        t_uid =t_obj['uid']
        t_area=t_obj['area']
        t_pid=t_obj['pid']
        t_data=ujson.loads(t_obj['data'])
        t_order_id = t_obj["order"]
    elif request.method == 'POST':
        t_uid = request.POST.get('uid')

    if not t_uid:
        response = HttpResponse("empty uid")
        return response

    t_res=[];
    for t_obj in t_data:
        #print t_obj
        money = t_obj["amt"]
        pay_info = HXHConsumerInfo()
        pay_info.uid = t_uid
        pay_info.amt = money
        pay_info.unit = t_obj['unit']
        t_time = time.localtime(int(t_obj['created']))
        pay_info.create_date = time.strftime('%Y-%m-%d %H:%M:%S',t_time)
        pay_info.save()
        t_res.append(pay_info.id)
    response = HttpResponse("result:"+ujson.dumps(t_res))
    return  response

#登陆日志
# uid u'd8f33838264a11e7994f5254000d335c'
# area u's1'
# pid u'TEST'
# reason u'U_FLOGIN'
# gid u'hxhunter'
# data u'[{"ip":"58.62.202.85","created":1492748858.4332458973}]'
# order u'd9f7267c264a11e7994f5254000d335c'
@csrf_exempt
def hxh_user_login(request):
    t_obj=fun.Decode_Request(request)
    fun.add_log(t_obj)

    t_uid=None
    t_data=[]
    if request.method == 'GET':
        t_uid = t_obj['uid']
        t_area=t_obj['area']
        t_pid=t_obj['pid']
        t_data=ujson.loads(t_obj['data'])[0]
        t_order_id = t_obj["order"]
    elif request.method == 'POST':
        t_uid = request.POST.get('uid')

    if not t_uid:
        response = HttpResponse("empty uid")
        return response

    lgoin_info = HXHUserLogin()
    lgoin_info.uid = t_uid
    lgoin_info.pf=t_pid
    lgoin_info.server_id=t_area
    lgoin_info.ip = t_data['ip']
    t_time = time.localtime(int(t_data['created']))
    lgoin_info.login_time = time.strftime('%Y-%m-%d %H:%M:%S',t_time)
    lgoin_info.save()

    response = HttpResponse("result:"+ujson.dumps(lgoin_info.id))
    return  response

#玩家在线
# http://127.0.0.1:8080/api/hxhunter/online?reason=B_GET_EQUIP_FRAG&gid=hxhunter&uid=d8f33838264a11e7994f5254000d335c&area=s1&data=%5B%7B%22created%22%3A1492748858.5831749439%7D%5D&pid=TEST&order=da1990cc264a11e7994f5254000d335c
# uid u'd8f33838264a11e7994f5254000d335c'
# area u's1'
# pid u'TEST'
# reason u'B_GET_EQUIP_FRAG'
# gid u'hxhunter'
# data u'[{"created":1492748858.5831749439}]'
# order u'da1990cc264a11e7994f5254000d335c'
@csrf_exempt
def hxh_user_online(request):
    t_obj=fun.Decode_Request(request)
    fun.add_log(t_obj)

    t_uid=None
    t_data=[]
    if request.method == 'GET':
        t_uid = t_obj['uid']
        t_area=t_obj['area']
        t_pid=t_obj['pid']
        t_data=ujson.loads(t_obj['data'])[0]
        t_order_id = t_obj["order"]
    elif request.method == 'POST':
        t_uid = request.POST.get('uid')

    if not t_uid:
        response = HttpResponse("empty uid")
        return response

    #查找
    user_arr = HXHUserInfo.objects.filter(uid=t_uid,pf=t_pid,server_id=t_area)
    if user_arr:
        user_info=user_arr[0]
        t_time = time.localtime(int(t_data['created']))
        user_info.game_time = time.strftime('%Y-%m-%d %H:%M:%S',t_time)
        user_info.save()
        response = HttpResponse("result:"+ujson.dumps(user_info.id))
    else:
        response = HttpResponse("result:0")
    return  response